package com.breeze.media.codec;

import android.content.res.AssetFileDescriptor;
import android.media.MediaCodec;
import android.media.MediaExtractor;
import android.media.MediaFormat;
import android.os.Bundle;
import android.util.Log;

import androidx.appcompat.app.AppCompatActivity;

import com.breeze.media.R;
import com.breeze.media.codec.teams.DummyMediaCodec;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

public class CodecActivity extends AppCompatActivity {
    private static final String TAG = "CodecActivity";
    public static final String AUDIO = "audio/";
    public static final String VIDEO = "video/";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_codec);
        if (test()) {
            return;
        }
        MediaFormat mediaFormat = null;
        MediaFormat mediaFormat2 = null;
        MediaExtractor extractor = new MediaExtractor();
        AssetFileDescriptor afd = getResources().openRawResourceFd(R.raw.test);
        try {
            extractor.setDataSource(afd);
            Log.i(TAG, "onCreate: count=" + extractor.getTrackCount());
            int i = 0;
            while (true) {
                Log.i(TAG, "onCreate: i=" + i);
                if (i >= extractor.getTrackCount()) {
                    break;
                }
                MediaFormat trackFormat = extractor.getTrackFormat(i);
                String string = trackFormat.getString(MediaFormat.KEY_MIME);
                if (string.startsWith(VIDEO)) {
                    extractor.selectTrack(i);
                    mediaFormat = trackFormat;
                    break;
                }
                if (string.startsWith(AUDIO)) {
                    extractor.selectTrack(i);
                    mediaFormat2 = trackFormat;
                }
                i++;
            }
            /**
             * mediaFormat={track-id=2, level=16384, mime=video/avc, profile=8, language=```, display-width=1440, csd-1=java.nio.HeapByteBuffer[pos=0 lim=9 cap=9], durationUs=8401677, display-height=3200, width=1440, color-range=2, max-input-size=211044, frame-rate=24, height=3200, csd-0=java.nio.HeapByteBuffer[pos=0 lim=19 cap=19]}
             * mediaFormat2={max-bitrate=320000, sample-rate=48000, track-id=1, durationUs=8107125, mime=audio/mp4a-latm, profile=2, channel-count=1, bitrate=320000, language=```, aac-profile=2, max-input-size=788, csd-0=java.nio.HeapByteBuffer[pos=0 lim=2 cap=2]}
             */
            Log.i(TAG, "onCreate: mediaFormat=" + mediaFormat + "\n mediaFormat2=" + mediaFormat2);

            MediaCodec decoder = MediaCodec.createDecoderByType(mediaFormat.getString("mime"));
            /*int dequeueInputBuffer = decoder.dequeueInputBuffer(5000);
            Log.i(TAG, "onCreate: dequeueInputBuffer=" + dequeueInputBuffer);
            if (dequeueInputBuffer > 0) {
                ByteBuffer byteBuffer = decoder.getInputBuffer(dequeueInputBuffer);
                byteBuffer.position(0);
                // byteBuffer.put(); // 写入数据
                decoder.queueInputBuffer(dequeueInputBuffer, 0, 1000, 1000, 0);
            }*/
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private boolean test() {

        boolean encoderOnly = false;
        boolean exynos = false;
        try {
            DummyMediaCodec dummyMediaCodec = new DummyMediaCodec(encoderOnly, exynos);
            dummyMediaCodec.init(640, 360, "nan");
            dummyMediaCodec.start();
            dummyMediaCodec.callbackCountDown.await(5, TimeUnit.SECONDS);
            dummyMediaCodec.stop();
        } catch (Exception exception) {

        }
        return true;
    }
}